package org.mcache;

import java.util.Objects;

/**
 * @Author: tjc
 * @description
 * @Date Created in 10:24 2022-08-07
 */
public class RedisString extends RedisObject {
    StringBuilder impl;

    public RedisString(String value) {
        impl = new StringBuilder(value);
    }

    public void append(String value) {
        impl.append(value);
    }

    public RedisString substring(int start, int end) {
        return new RedisString(impl.substring(start, end));
    }

    public int length() {
        return impl.length();
    }

    public String value() {
        return impl.toString();
    }

    @Override
    public RedisString checkRedisString() {
        return this;
    }

    @Override
    public boolean equals(Object o) {
        if (o == null || getClass() != o.getClass()) return false;
        RedisString that = (RedisString) o;
        if (this.impl == that.impl) return true;
        return Objects.equals(impl.toString(), that.impl.toString());
    }

    @Override
    public int hashCode() {
        return Objects.hash(impl.toString());
    }

    public int getInt() {
        return Integer.valueOf(impl.toString());
    }

    public void inc(RedisString o) {
        if (isNumber() && o.isNumber()) {
            impl = new StringBuilder(this.getInt() + o.getInt());
        }
    }

    public boolean isNumber() {
        return false;
    }

    @Override
    public boolean isRedisString() {
        return true;
    }

    @Override
    public String toString() {
        return impl.toString();
    }
}
